import React from "react";
import { withPropsAPI } from "gg-editor";
import { Button,message } from 'antd';
import styles from './index.less';
import { connect } from 'dva';

@connect(({ DialogAdd, loading }) => ({
  DialogAdd,
  loading: loading.effects['dialogAdd/fetchSlot'],
}))

class Save extends React.Component {
  state = {
    saveLoading:false,
    publishLoading: false,
    tranLoading: false,
  };

  handleClick = () => {
    const { propsAPI,dialogId } = this.props;
    console.log(propsAPI.save());
    this.setState({
      saveLoading:true
    },()=>{
      this.save(propsAPI.save(),dialogId)
    })
  };

  save =(data,dialogId) =>{
    const { dispatch } = this.props;
    dispatch({
      type: 'dialogAdd/saveDialog',
      payload: {
        dialogId,
        data
      },
      callback:(res)=>{
        if(!res.code){
          message.info(res.msg);
          this.setState({
            saveLoading:false
          })
        }
      }
    });
  };

  trainDialog=()=>{
    const { dispatch,dialogId } = this.props;
    this.setState({
      tranLoading:true
    },()=>{
      dispatch({
        type: 'dialogAdd/trainDialog',
        payload: {
          dialogId
        },
        callback:(res)=>{
          console.log(res);
          if(!res.code){
            message.info(res.msg)
            this.props.showTrain();
          };
          this.setState({
            tranLoading:false
          })
        }
      });
    })
  };

  publishDialog=()=>{
    const { dispatch,dialogId } = this.props;
    this.setState({
      publishLoading:true
    },()=>{
      dispatch({
        type: 'dialogAdd/publishDialog',
        payload: {
          dialogId
        },
        callback:(res)=>{
          console.log(res);
          if(!res.code){
            message.info(res.msg);
            this.setState({
              publishLoading:false
            })
          }
        }
      });
    });
  };

  render() {
    const {saveLoading,publishLoading,tranLoading} = this.state;
    return (
      <div className={styles.saveBtn}>
        <Button htmlType='button' loading={saveLoading} type="primary" className={styles.btn} onClick={this.handleClick}>保存</Button>
        <Button htmlType='button' className={styles.btn} loading={tranLoading} onClick={()=>this.trainDialog()}>训练</Button>
        {/*<Button htmlType='button' className={styles.btn} >调试</Button>*/}
        <Button htmlType='button' className={styles.btn} loading={publishLoading} onClick={()=>this.publishDialog()}>发布</Button>
      </div>
    );
  }
}

export default withPropsAPI(Save);
